package com.kll.tv.views.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.kll.carolinehmibiz.base.utils.L
import com.kll.carolinehmibiz.net.bean.GetFamilyResponse
import com.kll.tv.R
import com.kll.tv.databinding.ItemContactFamilyBinding

class ContactFamilyAdapter(var context: Context) : RecyclerView.Adapter<ContactFamilyAdapter.InnerHolder>() {
    private var mItemClickListener: OnFamilyListItemClickListener? = null

    // 数据
    var data: MutableList<GetFamilyResponse> = ArrayList()
        set(value) {
            if (value == null) {
                data.clear()
            } else {
                field = value
            }
            notifyDataSetChanged()
        }

    fun addData(data: ArrayList<GetFamilyResponse>) {
        if (data != null) {
            this.data.addAll(data)
            notifyDataSetChanged()
        }
    }

    inner class InnerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding = ItemContactFamilyBinding.bind(itemView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_contact_family, parent, false)
        return InnerHolder(view)
    }

    override fun onBindViewHolder(holder: InnerHolder, position: Int) {
        val data = data[position]

        L.d("TAG", "onBindViewHolder: ${position} ${this.data.size}")
        if (position == this.data.size - 1) {
            holder.binding.vLine.visibility = View.GONE
        } else {
            holder.binding.vLine.visibility = View.VISIBLE
        }

        // item可以获得焦点，需要设置这个属性。
        holder.itemView.setFocusable(true)
        holder.itemView.nextFocusLeftId = R.id.tv_contact_family

        holder.binding.tvFirstName.text = data.first_name
        holder.binding.tvFamilyName.text = data.first_name + data.last_name
        holder.binding.tvPhoneNumber.text = data.mobile
        Glide.with(holder.itemView)
            .load("http://" + data.avatar)
            .apply(RequestOptions().placeholder(R.drawable.icon_contact_family_head))
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    holder.binding.tvFirstName.visibility = View.VISIBLE
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    holder.binding.tvFirstName.visibility = View.GONE
                    return false
                }

            })
            .into(holder.binding.ivAvatar)
        /*if (holder.binding.ivVideo.isFocusable) {
            holder.binding.tvVideo.setTextColor(Color.parseColor("#008399"))
        } else {
            holder.binding.tvVideo.setTextColor(Color.parseColor("#B5B5B6"))
        }
        if (holder.binding.ivVoice.isFocusable) {
            holder.binding.tvVoice.setTextColor(Color.parseColor("#008399"))
        } else {
            holder.binding.tvVoice.setTextColor(Color.parseColor("#B5B5B6"))
        }*/

        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener {
                mItemClickListener!!.onFamilyItemClick(data)
            }
            holder.binding.ivVideo.setOnClickListener {
                mItemClickListener!!.onFamilyVideoClick(data.tencent_user_id)
            }
            holder.binding.ivVoice.setOnClickListener {
                mItemClickListener!!.onFamilyVoiceClick(data.tencent_user_id)
            }
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    interface OnFamilyListItemClickListener {
        fun onFamilyItemClick(data: GetFamilyResponse)
        fun onFamilyVideoClick(tecentUserId: String)
        fun onFamilyVoiceClick(tecentUserId: String)
    }

    fun setOnFamilyListItemClickListener(listener: OnFamilyListItemClickListener) {
        this.mItemClickListener = listener
    }

}